<?php


namespace app\index\business;


use app\common\model\ConsignorItem;
use app\common\model\ConsignorItemAddMember;
use app\common\model\ConsignorReplenish;
use app\common\model\LogisticsReplenish;
use app\common\model\MemberInfo;
use app\common\model\MemberInfoAddUser;
use think\Exception;

class consignorBus
{
    public $consignorItemModel = null;
    public $memberInfoAddUser = null;
    public $memberInfo = null;
    public $consignorReplenish= null;
    public $logisticsReplenish=null;
    public $consignorItemAddMember=null;
    public function __construct()
    {
        $this->consignorItemModel = new ConsignorItem();
        $this->memberInfoAddUser = new MemberInfoAddUser();
        $this->memberInfo = new MemberInfo();
        $this->consignorReplenish = new ConsignorReplenish();
        $this->logisticsReplenish = new LogisticsReplenish();
        $this->consignorItemAddMember = new ConsignorItemAddMember();
    }
    public function addNoticeItemBus($data){
        $result = $this->consignorItemModel->add($data);
        if(empty($result)){
            throw new Exception("添加失败，请查询数据");
        }
        return $result;
    }
    public function updateNoticeItemBus($where,$data){
        $result = $this->consignorItemModel->updateItem($where,$data);
        return $result;
    }
    public function getNoticeItemBus($where,$paging){
        $result = $this->consignorItemModel->getListByPage($where,$paging);
//        if(empty($result)){
//            throw new Exception("未查询到数据");
//        }
        return $result;
    }
    public function getItemByIdBus($id){

        $result = $this->consignorItemModel->getItemById($id);
        if(empty($result)){
            throw new Exception("获取失败，请检查数据库连接");
        }
        return $result;
    }

    /**
     * 根据发布项目的id和企业的member_id获取详情，防止其他企业可以访问
     * @param $where
     * @return array|\think\Model
     * @throws Exception
     */
    public function getNoticeDetailBus($where){

        $result = $this->consignorItemModel->getItemByWhere($where);
        if(empty($result)){
            throw new Exception("该项目正在审核或者无权访问");
        }
        return $result;
    }
    /**根据条件分页查询memberInfoAddUser中的数据
     * @param $where
     * @param $paging
      * @param $member_id
     * @return array
     * @throws Exception
     */
    public function getFacilitatorListBus($where,$paging,$member_id){
//        $where['status'] = 1;
        $whereData = [
            'member_id' => $member_id
        ];
        $res = $this->memberInfo->getMemberInfoById($whereData);
        if(empty($res)){
            throw new Exception('未查到改企业或者该企业正在审核');
        }

//        $where['member_info_id'] = $res['id'];
//        dump($res);exit();
        $result = $this->memberInfoAddUser->getListByPage($where,$paging);
        return $result;
    }

    public function changeFacilitatorFieldBus($where,$data){
        $result = $this->memberInfoAddUser->changeFacilitatorField($where,$data);
        if(empty($result)){
            throw new Exception("更改失败，请联系管理员");
        }
        return $result;
    }
    public function getFacilitatorDetailBus($member_info_member_id,$member_id){

//        $memberInfoWhere = [
//            'member_id' => $member_info_member_id,
//        ];
//        $memberInfoResult = $this->memberInfo->getMemberInfoById($memberInfoWhere);

        $whereData = [
          "member_id" => $member_id,
          "member_info_id" => $member_info_member_id,
           "status" => 2
        ];
        $result = $this->memberInfoAddUser->getListItem($whereData);
        if(empty($result)){
            throw new Exception("未加入该运力服务商或者未审核");
        }
        $memberDetailWhere = [
            "member_id" => $member_id
        ];
//        dump($memberDetailWhere);exit();
        $visitInfo = $this->memberInfo->getMemberInfoById($memberDetailWhere);
        if(empty($visitInfo)){
            throw new Exception("该会员已经删除或者正在审核中");
        }
        $supplement=null;
        if($visitInfo['member_category']==1){
            $supplement = $this->consignorReplenish->getReplenishInfoByWhere($memberDetailWhere);
        }
        if($visitInfo['member_category']==2){
            $supplement = $this->logisticsReplenish->getReplenishInfoByWhere($memberDetailWhere);
        }
        $res = [
            "memberInfo"=>$visitInfo,
            "supplement"=>$supplement
        ];
        return $res;
    }

    public function getConsignorItemAddMemberListPageBus($where,$paging){
        $result = $this->consignorItemAddMember->getListByPage($where,$paging);
        return $result;
    }
    public function getConsignorItemAddMemberDetailBus($member_id){
        $whereData = [
            'member_id' => $member_id
        ];
        $visitInfo = $this->memberInfo->getMemberInfoById($whereData);
        if(empty($visitInfo)){
            throw new Exception("该会员已经删除或者正在审核中");
        }
        $supplement=null;
        if($visitInfo['member_category']==1){
            $supplement = $this->consignorReplenish->getReplenishInfoByWhere($whereData);
        }
        if($visitInfo['member_category']==2){
            $supplement = $this->logisticsReplenish->getReplenishInfoByWhere($whereData);
        }
        $res = [
            "memberInfo"=>$visitInfo,
            "supplement"=>$supplement
        ];
        return $res;
    }
    public function changeConsignorItemAddMemberStatusBus($id){
        $where = [
            'id'=>$id
        ];
        $data = [
            'status'=>4
        ];
        $res = $this->consignorItemModel->updateItem($where,$data);
        return $res;
    }
    public function RemoveConsignorItemBus($id){
        $res = $this->consignorItemModel->removeItem($id);
        if(empty($res)){
            throw new Exception("该会员已经删除");
        }
        $this->consignorItemAddMember->removeItem($id);
        return $res;
    }

}